package es.upm.fi.sos.t3.shoppingcart.util;

import java.util.ArrayList;
import java.util.List;

public class Carrito {
	private List<Item> shopping; 
	
	public Carrito(){
		this.shopping = new ArrayList<Item>();
	}
	
	public void addItemCarrito(Item item){
		this.shopping.add(item);		
	}
	public void eliminarItemCarrito(Item item){
		int posItem=this.getPosItemCarrito(item);
		if(posItem != -1){
			this.shopping.remove(posItem);
		}		
	}
	public void actualizarItemCarrito(Item item){
		int posItem=this.getPosItemCarrito(item);
		this.shopping.add(posItem, item);	
	}
	
	public void limpiarCarrito(){
		this.shopping.clear();
	}
	public Item buscaItem(Item item){
		Item itemAux=null;
		int posItem=this.getPosItemCarrito(item);
		if(posItem!=-1){
			itemAux=this.shopping.get(posItem);
		}
		return itemAux;
	}
	public int getCountItemCar(){
		return shopping.size();
	}
	public List<Item> getListaItemCarritoAll(){
		return this.shopping;
	}
	public Double getTotalCarrito(){
		Double total=0.0;
		for(int i=0; i<this.shopping.size();i++){
			Item itemAux	=	this.shopping.get(i);
			total			=	total + itemAux.getSubTotal();
		}			
		return total;
	}
	private int getPosItemCarrito(Item item){
		int pos=-1;
		for(int i=0; i<this.shopping.size();i++){
			if(this.shopping.get(i).getNombreP().equals(item.getNombreP())){
				pos=i;
				break;
			}
		}
		return pos;
	}
	
}
